package com.cskaoyan.mall.controller;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.OrderRequestParam;
import com.cskaoyan.mall.model.OrderResponse;
import com.cskaoyan.mall.service.OrderService;
import com.cskaoyan.mall.service.OrderServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackgeName: ${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Author: admin
 * Date: 2020/5/5 19:40
 * project name: Javaproject1
 * @Version:
 * @Description:
 */
@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    Gson gson = new Gson();

    OrderService orderService = new OrderServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("//api/admin/order/", "");
        if ("ordersByPage".equals(action)) {
            ordersByPage(request,response);
        }
    }

    /**
     * 分页同时多条件查询显示订单
     * @param request
     * @param response
     */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        OrderRequestParam param = gson.fromJson(requestBody, OrderRequestParam.class);
        //进行一些校验

        try {
            if (!StringUtils.isEmpty(param.getMoneyLimit1())) {
                Double.parseDouble(param.getMoneyLimit1());
            }
            if (!StringUtils.isEmpty(param.getMoneyLimit2())) {
                Double.parseDouble(param.getMoneyLimit2());
            }
        } catch (Exception e) {
            //直接return一个10000
            return;
        }
        OrderResponse orderResponse = orderService.orderByPage(param);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
